class IndexedDBStore {
    constructor(dbName, version = 1) {
      this.dbName = dbName;
      this.version = version;
      this.db = null;
    }
  
    // 初始化数据库连接
    async open(storeConfig) {
      return new Promise((resolve, reject) => {
        const request = indexedDB.open(this.dbName, this.version);
  
        request.onupgradeneeded = (event) => {
          const db = event.target.result;
          storeConfig.forEach(config => {
            if (!db.objectStoreNames.contains(config.name)) {
              const store = db.createObjectStore(
                config.name, 
                { keyPath: config.keyPath, autoIncrement: config.autoIncrement ?? false }
              );
              config.indexes?.forEach(index => {
                store.createIndex(index.name, index.keyPath, { unique: index.unique ?? false });
              });
            }
          });
        };
  
        request.onsuccess = (event) => {
          this.db = event.target.result;
          resolve(this);
        };
  
        request.onerror = (event) => {
          reject(`Database open failed: ${event.target.error}`);
        };
      });
    }
  
    // 执行事务的核心方法
    _executeTransaction(storeName, mode = 'readonly', operation) {
      return new Promise((resolve, reject) => {
        const transaction = this.db.transaction(storeName, mode);
        const store = transaction.objectStore(storeName);
  
        // 事务级错误处理
        transaction.onerror = (event) => reject(event.target.error);
  
        // 执行操作并传递 resolve/reject
        const request = operation(store, resolve, reject);
  
        // 操作级错误处理（如果存在请求对象）
        if (request?.addEventListener) {
          request.onerror = (event) => reject(event.target.error);
        }
      });
    }
  
    // 添加数据
    async add(storeName, data) {
      return this._executeTransaction(storeName, 'readwrite', (store, resolve) => {
        const request = store.add(data);
        request.onsuccess = () => resolve(request.result);
        return request;
      });
    }
  
    // 获取单条数据
    async get(storeName, key) {
      return this._executeTransaction(storeName, 'readonly', (store, resolve) => {
        const request = store.get(key);
        request.onsuccess = () => resolve(request.result);
        return request;
      });
    }
  
    // 通过索引查询
    async getByIndex(storeName, indexName, value) {
      return this._executeTransaction(storeName, 'readonly', (store, resolve) => {
        const index = store.index(indexName);
        const request = index.get(value);
        request.onsuccess = () => resolve(request.result);
        return request;
      });
    }
  
    // 更新数据
    async update(storeName, data) {
      return this._executeTransaction(storeName, 'readwrite', (store, resolve) => {
        const request = store.put(data);
        request.onsuccess = () => resolve(request.result);
        return request;
      });
    }
  
    // 删除数据
    async delete(storeName, key) {
      return this._executeTransaction(storeName, 'readwrite', (store, resolve) => {
        const request = store.delete(key);
        request.onsuccess = () => resolve();
        return request;
      });
    }
  
    // 获取所有数据
    async getAll(storeName) {
      return this._executeTransaction(storeName, 'readonly', (store, resolve) => {
        const request = store.getAll();
        request.onsuccess = () => resolve(request.result);
        return request;
      });
    }
  
    // 游标遍历（支持条件过滤）
    async cursor(storeName, query = null, direction = 'next') {
      return this._executeTransaction(storeName, 'readonly', (store, resolve) => {
        const request = store.openCursor(query, direction);
        const results = [];
  
        request.onsuccess = (event) => {
          const cursor = event.target.result;
          if (cursor) {
            results.push(cursor.value);
            cursor.continue();
          } else {
            resolve(results);
          }
        };
        return request;
      });
    }
  
    // 清空存储
    async clear(storeName) {
      return this._executeTransaction(storeName, 'readwrite', (store, resolve) => {
        const request = store.clear();
        request.onsuccess = () => resolve();
        return request;
      });
    }
  }